
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.RegionUtil;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class CreateTitleContent {
    public static void createTitle(Workbook workbook,Sheet sheet){
        createTitleCell(sheet);
        titleWord(workbook,sheet);
        titleMerged(workbook,sheet);
    }
/*    public static void main(String[] args) throws IOException {
        //创建输出流
        FileOutputStream fos = new FileOutputStream("D:\\wenjian.xlsx");
        //创建工作蒲
        Workbook workbook = new XSSFWorkbook();
        //创建工作表
        Sheet sheet = workbook.createSheet("信息资源");

        //创建标准模板文件
        createTitleCell(sheet);
        titleWord(workbook,sheet);
        titleMerged(workbook,sheet);

        workbook.write(fos);
        fos.close();
    }*/
    //这个方法用来生成标题行的所有单元格，为了方便之后能直接使用sheet.getRow(i).getCell(i)方法得到Cell对象
    public static void createTitleCell(Sheet sheet){
        //第一层循环，i是行标
        for (int i=0; i<3; i++){
            Row row = sheet.createRow(i);
            //第二层循环，j是列标
            for (int j=0; j<19; j++){
                Cell cell = row.createCell(j);
                cell.setCellValue(7777);
            }
        }
    }

    //这个方法专门用来设置主标题的样式
    public static void setMainTitleStyle(Workbook workbook,Cell cell){
        Font font = workbook.createFont();
        //设置字体颜色
        //font.setColor(Font.COLOR_RED);
        //设置字体
        font.setFontName("黑体");
        font.setFontHeightInPoints((short) 16);
        //设置单元格样式
        CellStyle cellStyle = workbook.createCellStyle();
        //设为水平居中
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        //设为垂直居中
        cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        //将字体颜色设置加入单元格样式中
        cellStyle.setFont(font);
        //将单元格样式设置放入对应的单元格中
        cell.setCellStyle(cellStyle);
    }
    //这个方法用来设置列标题的样式和字体
    public static void titleStyle(Workbook workbook,Cell cell){
        Font font = workbook.createFont();
        //设置字体
        font.setFontName("宋体");
        //设置字号，字体大小
        font.setFontHeightInPoints((short) 9);
        //设置字体加粗
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

        //设置单元格样式
        CellStyle cellStyle = workbook.createCellStyle();
        //设为水平居中
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        //设为垂直居中
        cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        //设置自动换行
        cellStyle.setWrapText(true);
        //设置单元格背景颜色,设为浅灰色
        cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        //cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);

        //设置边框
        cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
        cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
        cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
        cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框

        //将字体颜色设置加入单元格样式中
        cellStyle.setFont(font);
        //将单元格样式设置放入对应的单元格中
        cell.setCellStyle(cellStyle);

    }

    //这个方法用来合并模板文件的标题行与列
    public static void titleMerged(Workbook workbook,Sheet sheet){
        //合并标题行
        setRegionBorder(new CellRangeAddress(0, 0, 0, 18),sheet,workbook);
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 18));

        //合并信息资源名称
        setRegionBorder(new CellRangeAddress(1, 2, 0, 0),sheet,workbook);
        sheet.addMergedRegion(new CellRangeAddress(1, 2, 0, 0));

        //合并信息资源格式
        setRegionBorder(new CellRangeAddress(1, 1, 1, 2),sheet,workbook);
        sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 2));

        //合并重点领域分类
        setRegionBorder(new CellRangeAddress(1,2,3, 3),sheet,workbook);
        sheet.addMergedRegion(new CellRangeAddress(1,2,3, 3));

        //合并所属系统名称
        setRegionBorder(new CellRangeAddress(1,2,4, 4),sheet,workbook);
        sheet.addMergedRegion(new CellRangeAddress(1,2,4, 4));

        //合并更新频率
        setRegionBorder(new CellRangeAddress(1,1,5, 6),sheet,workbook);
        sheet.addMergedRegion(new CellRangeAddress(1,1,5, 6));

        //循环合并后面的标题列
        for (int i=7; i<19; i++){
            setRegionBorder(new CellRangeAddress(1,2,i, i),sheet,workbook);
            sheet.addMergedRegion(new CellRangeAddress(1,2,i, i));
        }
    }
    //这个方法传入要合并的单元格，设置合并的单元格的边框
    public static void setRegionBorder(CellRangeAddress cellRangeAddress, Sheet sheet, Workbook wb)  {
        RegionUtil.setBorderLeft(1, cellRangeAddress, sheet, wb);
        RegionUtil.setBorderBottom(1, cellRangeAddress, sheet, wb);
        RegionUtil.setBorderRight(1, cellRangeAddress, sheet, wb);
        RegionUtil.setBorderTop(1, cellRangeAddress, sheet, wb);
    }
    //这个方法用来设置标题行的值和样式
    public static void titleWord(Workbook workbook,Sheet sheet){
        //设置列宽
        for(int i=0; i<=18; i++){
            //(列坐标，列宽的值)
            sheet.setColumnWidth(i,3000);
        }
        //设置行高
        sheet.setDefaultRowHeight((short) 320); //默认行高
        sheet.getRow(2).setHeight((short) 820);

        //设置主标题的样式和填入主标题
        Cell mainTitle = sheet.getRow(0).getCell(0);
        mainTitle.setCellValue("浙江省市级政务信息资源目录");
        setMainTitleStyle(workbook,mainTitle);

        Cell title1 = sheet.getRow(1).getCell(0);
        title1.setCellValue("信息资源名称（必填项）");
        titleStyle(workbook,title1);

        Cell title2 = sheet.getRow(1).getCell(1);
        title2.setCellValue("信息资源格式");
        titleStyle(workbook,title2);

        Cell title3 = sheet.getRow(2).getCell(1);
        title3.setCellValue("信息资源格式（信息资源格式分类必填项-单选）");
        titleStyle(workbook,title3);

        Cell title4 = sheet.getRow(2).getCell(2);
        title4.setCellValue("信息资源格式（必填项-单选）");
        titleStyle(workbook,title4);

        Cell title5 = sheet.getRow(1).getCell(3);
        title5.setCellValue("重点领域分类（必填，多选,用英文,隔开）");
        titleStyle(workbook,title5);

        Cell title6 = sheet.getRow(1).getCell(4);
        title6.setCellValue("所属系统名称（必填，根据信息系统清单）");
        titleStyle(workbook,title6);

        Cell title7 = sheet.getRow(1).getCell(5);
        title7.setCellValue("更新频率");
        titleStyle(workbook,title7);

        Cell title8 = sheet.getRow(2).getCell(5);
        title8.setCellValue("更新频率（必填项-单选）");
        titleStyle(workbook,title8);

        Cell title9 = sheet.getRow(2).getCell(6);
        title9.setCellValue("具体更新时间（必填项）");
        titleStyle(workbook,title9);

        Cell title10 = sheet.getRow(1).getCell(7);
        title10.setCellValue("数据项（必填项）");
        titleStyle(workbook,title10);

        Cell title11 = sheet.getRow(1).getCell(8);
        title11.setCellValue("英文名称（选填项）");
        titleStyle(workbook,title11);

        Cell title12 = sheet.getRow(1).getCell(9);
        title12.setCellValue("字段描述（选填项）");
        titleStyle(workbook,title12);

        Cell title13 = sheet.getRow(1).getCell(10);
        title13.setCellValue("数据类型（必填项-单选）");
        titleStyle(workbook,title13);

        Cell title14 = sheet.getRow(1).getCell(11);
        title14.setCellValue("数据长度（选填项）");
        titleStyle(workbook,title14);

        Cell title15 = sheet.getRow(1).getCell(12);
        title15.setCellValue("是否字典项（选填项）");
        titleStyle(workbook,title15);

        Cell title16 = sheet.getRow(1).getCell(13);
        title16.setCellValue("是否可为空（选填项）");
        titleStyle(workbook,title16);

        Cell title17 = sheet.getRow(1).getCell(14);
        title17.setCellValue("是否主键（必需要有一个主键）");
        titleStyle(workbook,title17);

        Cell title18 = sheet.getRow(1).getCell(15);
        title18.setCellValue("默认值（选填项）");
        titleStyle(workbook,title18);

        Cell title19 = sheet.getRow(1).getCell(16);
        title19.setCellValue("共享属性（必填项-单选）");
        titleStyle(workbook,title19);

        Cell title20 = sheet.getRow(1).getCell(17);
        title20.setCellValue("共享条件（必填项）");
        titleStyle(workbook,title20);

        Cell title21 = sheet.getRow(1).getCell(18);
        title21.setCellValue("开放属性（必填项）");
        titleStyle(workbook,title21);

    }

}
